﻿@model NavigatorBarModel
@{
    Layout = "_Default";
    ViewData["Title"] = "Transport";
}

@section javascript {
    <script>

        var $dialogNew = $("#dialogNew");
        var $btnSubmit = $('#btnSubmit');
        var $dataForm = $('#dataForm');
        var $TableGroup = $("#TableGroup");
        var $editState = true;
        var TableArrar = new Array();

        $(function () {

            //1.初始化Table
            var oTable = new TableInit();
            oTable.Init();

            $('#btn_query').on('click', function () {
                $("#TableGroup").bootstrapTable('refresh');
            });

            $('.btn_add').on('click', function () {
                $editState = false;
                $('#myModalLabel').html('任务添加窗口');

                $dialogNew.find(".selectpicker").selectpicker('val', -1);
                $dialogNew.find(".form-control").val("");
                $('#dialogNew').modal();
            });

            $btnSubmit.click(function () {
                var requesturl = '/api/Task/add';
                if ($editState)
                    requesturl = '/api/Task/edit';
                $dialogNew.modal('hide');
                Request("put", requesturl, serializeNotNull($dataForm.serialize()));
            });

            function serializeNotNull(serStr) {
                return serStr.split("&").filter(str => !str.endsWith("=")).join("&");
            }

            function Request(method, url, Data) {
                $.ajax({
                    type: method,
                    url: url,
                    data: Data,
                    datatype: 'json',
                    success: function (res) {
                        if (res.state == 200) {
                            $TableGroup.bootstrapTable('refresh');
                            toastr.success(res.message);
                        }
                        else {
                            toastr.error(res.message);
                        }
                    },
                    error: function (e) {
                        toastr.error(e.responseText);
                    }
                });
            }

            $('.btn_delete').on('click', function () {
                var data = $("#TableGroup").bootstrapTable('getSelections');
                if (data.length > 0) {
                    deleteRow(data);
                } else {
                    toastr.error("请选中后操作！");
                }
            });

            $('.tb_edit').click(function () {
                var data = $("#TableGroup").bootstrapTable('getSelections');
                if (data.length == 1) {
                    if (data[0].id != 0)
                        edit(data[0].id);
                    else console.log(data[0].id);
                } else {
                    toastr.error("请选中一行后编辑！");
                }
            });
        });

        function edit(index) {
            $editState = true;
            var Info = TableArrar[index]
            if (Info != null) {
                jQuery.each(Info, function (i, val) {
                    $('#' + i + '').val(val);
                    $('#' + i + '').selectpicker('val', val)
                });
                $('#dialogNew').modal();
            }
        }

        function deleteRow(data) {
            console.log(data);
            bootbox.confirm({
                message: "确认删除吗？",
                buttons: {
                    confirm: {
                        label: '确认',
                        className: 'btn-primary'
                    },
                    cancel: {
                        label: '取消',
                        className: 'btn-default'
                    }
                },
                callback: function (result) {
                    if (result) {
                        $.ajax({
                            url: '/api/Task',
                            type: "delete",
                            data: JSON.stringify(data),
                            contentType: 'application/json',
                            success: function (res) {
                                if (res.state == 200) {
                                    $("#TableGroup").bootstrapTable('refresh');
                                    toastr.success(res.message);
                                }
                                else {
                                    $("#TableGroup").bootstrapTable('refresh');
                                    toastr.error(res.message);
                                }
                            },
                            error: function (e) {
                                toastr.error(e.responseText);
                            }
                        });
                    }
                }
            });
        }

        function deleteTool(id) {
            deleteRow([{ "id": id }]);
        }
        var TableInit = function () {
            var oTableInit = new Object();
            //初始化Table
            oTableInit.Init = function () {
                $('#TableGroup').bootstrapTable({
                    url: '/api/Task',                  //请求后台的URL（*）
                    method: 'get',                      //请求方式（*）
                    toolbar: '#toolbar',                //工具按钮用哪个容器
                    striped: true,                      //是否显示行间隔色
                    cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                    pagination: true,                   //是否显示分页（*）
                    sortable: false,                     //是否启用排序
                    sortOrder: "asc",                   //排序方式
                    queryParams: oTableInit.queryParams,//传递参数（*）
                    sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                    pageNumber: 1,                       //初始化加载第一页，默认第一页
                    pageSize: 10,                       //每页的记录行数（*）
                    pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
                    search: false,                       //是否显示表格搜索，此搜索是客户端搜索，
                    strictSearch: true,
                    showColumns: true,                  //是否显示所有的列
                    showRefresh: true,                  //是否显示刷新按钮
                    minimumCountColumns: 2,             //最少允许的列数
                    clickToSelect: true,                //是否启用点击选中行
                    /*    height: 500,            */            //行高，如果没有设置height属性，表格自动根据记录条数决定表格高度
                    uniqueId: "id",                     //每一行的唯一标识，一般为主键列
                    showToggle: true,                    //是否显示详细视图和列表视图的切换按钮
                    cardView: false,                    //是否显示详细视图
                    detailView: false,
                    columns: [
                        {
                            checkbox: true
                        }, {
                            field: 'id',
                            title: '任务ID',
                            align: 'center'
                        }, {
                            field: 'index',
                            title: 'index',
                            align: 'center'
                        }
                        , {
                            field: 'phase',
                            title: '任务阶段',
                            align: 'center', formatter: function (value) {
                                if (value != null) {
                                    switch (value) {
                                        case -1:
                                            return "<span class='badge badge-warning' style='width:80px;'>备料</span>";
                                        case 0:
                                            return "<span class='badge badge-primary' style='width:80px;'>等待中</span>";
                                        case 1:
                                            return "<span class='badge badge-primary' style='width:80px;'>已接受</span>";
                                        case 3:
                                            return "<span class='badge badge-primary' style='width:80px;'>到达取货点</span>";
                                        case 5:
                                            return "<span class='badge badge-primary' style='width:80px;'>取货完毕</span>";
                                        case 7:
                                            return "<span class='badge badge-primary' style='width:80px;'>到达放货点</span>";
                                        case 9:
                                            return "<span class='badge badge-primary' style='width:80px;'>放货完毕</span>";
                                        case 10:
                                            return "<span class='badge badge-success' style='width:80px;'>任务完成</span>";
                                        case 20:
                                            return "<span class='badge badge-success' style='width:80px;'>等待确认</span>";
                                        default:
                                            return "<span class='badge badge-dark' style='width:80px;'>" + value + "</span>";
                                    }

                                }
                            }
                        }, {
                            field: 'ikey',
                            title: 'ikey',
                            align: 'center'
                        }, {
                            field: 'startingPoint',
                            title: '起点',
                            align: 'center'
                        }, {
                            field: 'type',
                            title: '任务类型',
                            align: 'center'
                        }, {
                            field: 'endPoint',
                            title: '终点',
                            align: 'center'
                        }, {
                            field: 'interactiveState',
                            title: '交互状态',
                            align: 'center'
                        }, {
                            field: "agvId", title: "操作", width: 200, align: "center", formatter: function (value, row, index) {
                                TableArrar[row.id] = row;
                                var strHtml = '<button type="button" class="btn btn-primary btn-xs" onclick="edit(' + row.id + ')">编辑</button>&nbsp;' +
                                    '<button type="button" class="btn btn-danger btn-xs" onclick="deleteTool(' + row.id + ')">删除</button>';
                                return strHtml;
                            },
                            edit: false
                        }]
                });
            };
            //得到查询的参数
            oTableInit.queryParams = function (params) {
                var temp = {     //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                    limit: params.limit,   //页面大小
                    offset: params.offset  //页码
                };
                return temp;
            };
            return oTableInit;
        };
    </script>
}

<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1 class="m-0 text-dark">Ndc任务管理</h1>
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="@CoreConfiguration.DefaultMain">首页</a></li>
                    <li class="breadcrumb-item active">Ndc任务管理</li>
                </ol>
            </div>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-header">
        查询结果
    </div>
    <div class="card-body">
        <div class="alert alert-warning" role="alert">
            <span>请谨慎修改</span>
        </div>
        <div id="toolbar" class="btn-group">
            <div class="toolbar btn-group">
                <button type="button" class="btn btn-info btn_add"><i class="fa fa-plus" aria-hidden="true"></i><span>新增</span></button>
                <button type="button" class="btn btn-primary tb_edit"><i class="fa fa-pencil" aria-hidden="true"></i><span>编辑</span></button>
                <button type="button" class="btn btn-danger btn_delete"><i class="fa fa-remove" aria-hidden="true"></i><span>删除</span></button>
            </div>
        </div>
        <table id="TableGroup"></table>
        <div class="clearfix"></div>
    </div>
</div>
@section modal {
    <div class="modal-header">
        <h5 class="modal-title" id="myModalLabel">编辑窗口</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    <div class="modal-body">
        <form class="form-inline" id="dataForm">
            <div class="form-row">
                <input type="hidden" id="id" name="id" value="0" />

                <div class="form-group col-sm-6">
                    <label class="control-label" for="parentName">任务起点</label>
                    <input type="text" class="form-control flex-sm-fill" id="startingPoint" name="startingPoint" placeholder="不可为空" data-valid="true" />
                </div>

                <div class="form-group col-sm-6">
                    <label class="control-label" for="parentName">任务类型</label>
                    <input type="text" class="form-control flex-sm-fill" id="type" name="type" placeholder="不可为空" data-valid="true" />
                </div>

                <div class="form-group col-sm-6">
                    <label class="control-label" for="parentName">任务终点</label>
                    <input type="text" class="form-control flex-sm-fill" id="endPoint" name="endPoint" placeholder="不可为空" data-valid="true" />
                </div>

                <div class="form-group col-sm-6">
                    <label class="control-label" for="parentName">index</label>
                    <input type="text" class="form-control flex-sm-fill" id="index" name="index" placeholder="可为空" />
                </div>
                <div class="form-group col-sm-6">
                    <label class="control-label" for="parentName">phase</label>
                    <input type="text" class="form-control flex-sm-fill" id="phase" name="phase" placeholder="可为空" />
                </div>
            </div>
        </form>
    </div>
}